package com.happyass.service.impl;

import com.happyass.dao.RoleDao;
import com.happyass.pojo.Role;
import com.happyass.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleDao roleDao;

    @Override
    public List<Role> getRoles() {
        return roleDao.findAll();
    }

    public Optional<Role> getRoleById(Integer RoleId) {
        return roleDao.findById(RoleId);
    }

    @Override
    public boolean addRole(Role role) {
        try {
            Role role1 = new Role();
            role1.setRoleName(role.getRoleName());
            role1.setResources(role.getResources());
            roleDao.save(role1);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean delRole(Integer RoleId) {

        try {
            roleDao.deleteById(RoleId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateRole(Role role) {
        try {
            Role role1 = roleDao.findById(role.getRoleId()).get();
            role1.setRoleName(role.getRoleName());
            roleDao.save(role1);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateRoleByStaffId(Integer staffId, Integer roleId) {

        int i = roleDao.updateRoleByStaffId(staffId, roleId);
        if (i == 1) {
            return true;
        }
        return false;
    }
}
